<?php

namespace app\index\controller;
use app\index\model\Notice as NoticeModal;
use app\index\model\SystemNotice as SystemNoticeModal;
use app\BaseController;
use think\Request;
class Notice extends BaseController
{
    protected $middleware = [
        'Auth' => ['except' => []]
    ];

    public function statistics(Request $request)
    {
        $sql = new NoticeModal();
        $sysInfo = $sql
            ->where('userId', $request->user->id)->where('type', 'in', ['REPORT_SUCCESS'])->order('create_at desc')->findOrEmpty();
        $commentInfo = $sql
            ->where('userId', $request->user->id)->where('type', 'in', ['REPLY_EXCHANGE'])->order('create_at desc')->findOrEmpty();
        $sysCount  = $sql
            ->where('userId', $request->user->id)->where('type', 'in', ['REPORT_SUCCESS'])->where('is_read', 0)->count();
        $commentCount = $sql
            ->where('userId', $request->user->id)->where('type', 'in', ['REPLY_EXCHANGE'])->where('is_read', 0)->count();
        $info = [];
        if($sysInfo->isEmpty()) {
            $info['notice'] = [
                'create_at' => '',
                'desc' => '',
                'count' => 0
            ];
        } else {
            $info['notice'] = [
                'create_at' => date("m/d H:i", strtotime($sysInfo['create_at'])),
                'desc' => $sysInfo['content'],
                'count' => $sysCount
            ];
        }
        if($commentInfo->isEmpty()) {
            $info['comment'] = [
                'create_at' => '',
                'desc' => '',
                'count' => 0
            ];
        } else {
            $info['comment'] = [
                'create_at' => date("m/d H:i", strtotime($commentInfo['create_at'])),
                'desc' => $commentInfo['content'],
                'count' => $commentCount
            ];
        }
        $systemSql = new SystemNoticeModal();
        $systemInfo = $systemSql->order('create_at desc')->find();
        $info['system'] = [
            'create_at' => date("m/d H:i", strtotime($commentInfo['create_at'])),
            'title' => $systemInfo['title']
        ];
        return result()->success($info);
    }

    public function systemList()
    {
        $params = $this->request->post(['current', 'size']);
        $sql = new SystemNoticeModal();
        $list = $sql
            ->order('create_at desc')
            ->page($params['current'])
            ->limit($params['size'])
            ->select();
        return result()->success($list);
    }

    public function readAll(Request $request)
    {
        $params = $this->request->post(['type']);
        if($params['type'] == 'system') {
            $types = ['REPORT_SUCCESS'];
        } else {
            $types = ['REPLY_EXCHANGE'];
        }
        $sql = new NoticeModal();
        $sql
            ->where('userId', $request->user->id)->where('type', 'in', $types)->where('is_read', 0)->update([
                'is_read' => 1
            ]);
        return result()->success("已全部已读");
    }
    public function readOne(Request $request)
    {
        $params = $this->request->post(['id']);
        $sql = new NoticeModal();
        $sql
            ->where('userId', $request->user->id)->where('id', $params['id'])->where('is_read', 0)->update([
                'is_read' => 1
            ]);
        return result()->success("设置已读成功");
    }

    public function list(Request $request)
    {
        $params = $this->request->post(['current', 'size', 'types']);
        $params['user_id'] = $request->user->id;
        $sql = new NoticeModal();
        $whereArr[] = ['userId', '=', $params['user_id']];
        $whereArr[] = ['type', 'in', $params['types']];
        $list = $sql
            ->where($whereArr)
            ->with(['user', 'info'])
            ->page($params['current'])
            ->limit($params['size'])
            ->order('create_at desc')
            ->select();
        foreach ($list as $key => $item) {
            $list[$key]['new_at'] = date("m/d H:i", strtotime($item['create_at']));
        }
        return result()->success($list);
    }
}